package com.itcc.dao.impl;

import com.itcc.dao.CartDao;
import com.itcc.entity.Cart;
import com.itcc.entity.Product;
import com.itcc.utils.C3P0Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class CartDaoImpl implements CartDao {
    @Override
    public Cart hasCart(int uid, String pid) throws InvocationTargetException, IllegalAccessException, SQLException {

        QueryRunner queryRunner=new QueryRunner(C3P0Utils.getDataSource());

        String sql = "select p.p_name as pname,p.p_id as pid,p.t_id as tid," +
                "p.p_time as ptime,p.p_image as pimage,p_state as pstate," +
                "p.p_info as pinfo, p.p_price as pprice,c.c_id as cid,c.u_id as uid,c.c_count as ccount," +
                "c.c_num as cnum from product p join cart c on p.p_id = c.p_id where" +
                " c.u_id = ? and c.p_id = ?;";

        Map<String, Object> query = queryRunner.query(sql, new MapHandler(), uid, pid);

        if(query==null){
            return null;
        }




            Cart cart = new Cart();
            Product product = new Product();

            product.setPid((Integer) query.get("p_id"));
            product.setPname((String) query.get("p_name"));
            product.setPtime((Date) query.get("p_time")); // 可能需要类型转换
            product.setPimage((String) query.get("p_image"));
            product.setPstate((Integer) query.get("p_state"));
            product.setPinfo((String) query.get("p_info"));
            product.setPprice((BigDecimal) query.get("p_price"));

            cart.setCid((Integer) query.get("c_id"));
            cart.setUid((Integer) query.get("u_id"));
            cart.setCnum((Integer) query.get("c_num"));
            cart.setCcount((BigDecimal) query.get("c_count"));
            cart.setProduct(product);
        cart.setProduct(product);
        return cart;
    }

    @Override
    public void updateCart(Cart cart) throws SQLException {

        QueryRunner queryRunner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="update cart set c_num=? ,c_count=? where c_id=? ";

        queryRunner.update(sql,cart.getCnum(),cart.getCcount(),cart.getCid());

    }

    @Override
    public void insertCart(Cart cart) throws SQLException {

        QueryRunner queryRunner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="insert into cart (u_id,p_id,c_num,c_count) values (?,?,?,?)";

        queryRunner.update(sql,cart.getUid(),cart.getProduct().getPid(),cart.getCnum(),cart.getCcount());

    }

    @Override
    public List<Cart> selectCartsByUid(int uid) throws InvocationTargetException, IllegalAccessException, SQLException {
        QueryRunner queryRunner=new QueryRunner(C3P0Utils.getDataSource());

        String sql = "select p.p_name as pname,p.p_id as pid,p.t_id as tid," +
                "p.p_time as ptime,p.p_image as pimage,p_state as pstate," +
                "p.p_info as pinfo, p.p_price as pprice,c.c_id as cid,c.u_id as uid,c.c_count as ccount," +
                "c.c_num as cnum from product p join cart c on p.p_id = c.p_id where" +
                " c.u_id = ?;";


        List<Map<String, Object>> list = queryRunner.query(sql, new MapListHandler(), uid);

        if(list==null){
            return null;
        }

        List<Cart> carts=new ArrayList<>();

        for (Map<String, Object> map : list) {

            Cart cart = new Cart();
            Product product = new Product();

            product.setPid((Integer) map.get("p_id"));
            product.setPname((String) map.get("p_name"));
            product.setPtime((Date) map.get("p_time")); // 可能需要类型转换
            product.setPimage((String) map.get("p_image"));
            product.setPstate((Integer) map.get("p_state"));
            product.setPinfo((String) map.get("p_info"));
            product.setPprice((BigDecimal) map.get("p_price"));

            cart.setCid((Integer) map.get("c_id"));
            cart.setUid((Integer) map.get("u_id"));
            cart.setCnum((Integer) map.get("c_num"));
            cart.setCcount((BigDecimal) map.get("c_count"));
            cart.setProduct(product);
            carts.add(cart);
        }
        return  carts;
    }

    @Override
    public void deleteCartByCid(String cid) throws SQLException {
        QueryRunner queryRunner=new QueryRunner(C3P0Utils.getDataSource());

        String sql="delete from cart where c_id=?;";

        queryRunner.update(sql,cid);
    }

    @Override
    public void updateByCid(BigDecimal count, BigDecimal cnumbig, String cid) throws SQLException {

        QueryRunner queryRunner=new QueryRunner(C3P0Utils.getDataSource());

            String sql="update cart set c_count=?,c_num=? where c_id=?;";

            queryRunner.update(sql,count,cnumbig,cid);
    }

    @Override
    public void deleteCartByUid(String uid) throws SQLException {

        QueryRunner queryRunner=new QueryRunner(C3P0Utils.getDataSource());

        String sql="delete from cart where u_id=?;";

        queryRunner.update(sql,uid);
    }
}
